<template>
  <div id="addStaff">
    <el-dialog
        title="新增用户"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="closeBtn"
    >
      <el-form ref="formRef" :rules="formRules" :model="userInfo" label-width="80px">
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/backUser/avatarImgUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="userInfo.phone"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="closeBtn">取 消</el-button>
    <el-button type="primary" @click="addStaffBtn('formRef')">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AddStaff",
  data() {
    return {
      //头像地址
      imageUrl: "",

      //用户信息
      userInfo:{},

      dialogVisible: true,

      // 表单校验规则
      formRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur'},
          { min: 5, max: 8, message: '请输入5~8个字符', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur'},
          { min: 6, max: 10, message: '请输入6~10个字符', trigger: 'blur'}
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur'},
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    //添加
    addStaffBtn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('/backUser/addBackUser',this.userInfo);
          this.$message.success(res);
          //自定义事件处理界面刷新
          this.$emit('query');
          this.$router.push('/staff');
        } else {
          this.$message.error('校验失败！')
        }
      })

    },
    //头像成功上传的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = res;
      this.userInfo.avatarImg = this.imageUrl
    },
    //上传之前的校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //关闭
    closeBtn() {
      this.$router.push('/staff');
    },
  },
  created() {

  }
}
</script >

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 78px;
  height: 78px;
  display: block;
}
</style>